//
//  SATutorialVC.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/1/26.
//

#import "SATutorialVC.h"
#import "SATutorialCell.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVKit/AVKit.h>
#import "SACommonCourseAPI.h"
#import "SATutorialModel.h"

@interface SATutorialVC ()<UITableViewDelegate, UITableViewDataSource, HTTPAPIDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<SATutorialModel *> *dataMarr;
@property (nonatomic, strong) NSMutableArray *playNameMarr;
@property (nonatomic, strong) AVPlayerViewController *playerVC;
@property (nonatomic, strong) SACommonCourseAPI *commonCourseAPI;

@end

@implementation SATutorialVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    if (self.commonCourseAPI == api) {
        self.dataMarr = [SATutorialModel mj_objectArrayWithKeyValuesArray:data[@"data"]];
        [self.tableView reloadData];
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
   
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataMarr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SATutorialCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SATutorialCell" forIndexPath:indexPath];
    cell.titleLab.text = self.dataMarr[indexPath.section].tutorialTitle;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSURL *url = [NSURL fileURLWithPath:self.dataMarr[indexPath.section].url];
    self.playerVC.player = [AVPlayer playerWithURL:url];
    [self.playerVC.player play];
    self.playerVC.videoGravity = AVLayerVideoGravityResizeAspect;
    [self presentViewController:self.playerVC animated:YES completion:nil];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(40);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:@"新手教程"];
    UIView *headerView = [[UIView alloc] initWithFrame:SARectMake(0, 0, kBaseScreenWidth, 10)];
    headerView.backgroundColor = kLineColor;
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = headerView;
    [self.commonCourseAPI sendRequestToServerWithActivityViewAndMask:POST];
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight)) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kLineColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SATutorialCell class] forCellReuseIdentifier:@"SATutorialCell"];
    }
    return _tableView;
}

-(AVPlayerViewController *)playerVC {
    if (!_playerVC) {
        _playerVC = [[AVPlayerViewController alloc] init];
        _playerVC.view.frame = SARectMake(0, StatusHeight, kBaseScreenWidth, 200);
    }
    return _playerVC;
}

-(SACommonCourseAPI *)commonCourseAPI {
    if (!_commonCourseAPI) {
        _commonCourseAPI = [SACommonCourseAPI apiWithDelegate:self];
    }
    return _commonCourseAPI;
}

-(NSMutableArray<SATutorialModel *> *)dataMarr {
    if (!_dataMarr) {
        _dataMarr = [NSMutableArray array];
    }
    return _dataMarr;
}

@end
